@extends('layouts.admin.application')

@section('content')
    <div class="main-content">
        <div class="col-xs-12">
            <div class="breadcrumbs" id="breadcrumbs">
                <ul class="breadcrumb">
                    <li><i class="icon-home home-icon"></i><a href="#">首页</a></li>
                    <li>品牌管理</li>
                </ul>
            </div>
            <div class="table-responsive">
                <div id="sample-table-2_wrapper" class="dataTables_wrapper" role="grid">
                    <div class="row">

                        @include('layouts.partials._flash')

                        <div class="col-sm-2">
                            <div id="sample-table-2_length" class="dataTables_length">
                                <a href="{{ route('cashier.brand.create') }}" class="btn btn-sm btn-primary">新增</a>
                                <a href="javascript:void(0);" class="btn btn-sm btn-purple del_all">删除</a>
                            </div>
                        </div>

                        <form>
                            <div class="col-sm-10">
                                <div class="dataTables_filter" id="sample-table-2_filter">
                                    <label>
                                        品牌名称:<input type="text" name="keyword" aria-controls="sample-table-2"
                                                    placeholder="按品牌名" value="{{Request::input('keyword')}}">
                                    </label>
                                    <label>
                                        <button type="submit" class="btn btn-xs btn-info" id="search">确定</button>
                                        <a href="{{ route('cashier.brand.index') }}"
                                           class="btn btn-xs btn-default">重置</a>
                                    </label>
                                </div>
                            </div>
                        </form>

                    </div>
                    <table id="sample-table-2"
                           class="table table-striped table-bordered table-hover dataTable del-table"
                           aria-describedby="sample-table-2_info">
                        <thead>
                        <tr role="row">
                            <th class="center">
                                <label>
                                    <input type="checkbox" class="ace" id="check_all">
                                    <span class="lbl"></span>
                                </label>
                            </th>
                            <th>编号</th>
                            <th>品牌名称</th>
                            <th>品牌链接</th>
                            <th>排序</th>
                            <th>是否显示</th>
                            <th>品牌商品</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        @foreach($brands as $brand)
                            <tr class="odd" data-id="{{$brand->id}}">
                                <td class="center">
                                    <label>
                                        <input type="checkbox" class="ace checked_id" name="checked_id[]"
                                               value="{{$brand->id}}">
                                        <span class="lbl"></span>
                                    </label>
                                </td>
                                <td>{{$brand->id}}</td>
                                <td>{{$brand->name}}</td>
                                <td><a href="{{$brand->url}}">{{$brand->url}}</a></td>

                                <td>
                                    <input type="text" class="ace sort_order" value="{{$brand->sort_order}}"
                                           style="width: 50px;">
                                </td>
                                <td>
                                    {!! is_something($brand,'is_show') !!}
                                </td>
                                <td>
                                    {!! show_brand_products($brand) !!}
                                </td>
                                <td>
                                    <div class="visible-md visible-lg hidden-sm hidden-xs action-buttons">
                                        <a class="green" href="{{ route('cashier.brand.edit', $brand->id) }}"><i
                                                    class="icon-pencil bigger-130"></i></a>
                                        <a class="red" href="{{route('cashier.brand.destroy', $brand->id)}}"
                                           data-method="delete" data-token="{{csrf_token()}}"
                                           data-confirm="确定要删除此条记录么?">
                                            <i class="icon-trash bigger-130"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                    <div class="row">
                        <div class="col-sm-6">共 {{$brands->total()}} 条记录</div>
                        <div class="col-sm-6">
                            <div class="dataTables_paginate paging_bootstrap">
                                <ul class="pagination">
                                    {{ $brands->appends(Request::all())->links() }}
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('js')
    <script>
        $(function () {
            //ajax排序
            $('.sort_order').change(function () {
                var data = {
                    id: $(this).parents('tr').data('id'),
                    sort_order: $(this).val()
                };

                $.ajax({
                    type: 'PATCH',
                    url: '{{ route('cashier.brand.sort_order') }}',
                    data: data,
                    success: function (data) {
                        window.location.reload();
                    }
                });

                return false;
            });

            //改变属性
            $('.change_attr').click(function () {
                var _this = $(this);
                var data = {
                    id: $(this).parents('tr').data('id'),
                    attr: $(this).attr('data-attr')
                };

                $.ajax({
                    type: 'PATCH',
                    url: '{{ route('cashier.brand.change_attr') }}',
                    data: data,
                    success: function (data) {
                        _this.toggleClass('icon-ok icon-ban-circle');
                    }
                });

                return false;
            });

            //全选，反选
            $("#check_all").click(function () {
                $(':checkbox').prop("checked", this.checked);
            });

            //多选删除
            $('.del_all').click(function () {
                var length = $(".checked_id:checked").length;
                if (length == 0) {
                    alert("您必须至少选中一条!");
                    return false;
                }

                if (confirm('确定要删除么？')) {
                    var checked_id = $(".checked_id:checked").serialize();

                    $.ajax({
                        type: "DELETE",
                        url: "{{route('cashier.brand.delete_more')}}",
                        data: checked_id,
                        success: function () {
                            location.href = location.href;
                        }
                    });

                }
            });


        })
    </script>
@endsection